/**
    Die Ewigen Extension
    Copyright (C) 2011 DonKrieg @ sonkyu@googlemail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

var COON_FIELD_NAME     = "ignicks";           // input
var COON_FIELD_SERVER   = "server";            // input
var COON_FIELD_REPORT   = "kb_string";         // textarea
var COON_SEND           = "Abschicken"         // input-button

chrome.extension.sendRequest({action: "getFightReport", page: "coon"}, function(response) {
    // no report found
    if(response == undefined || response.report == undefined || response.report == "undefined") {
        return;
    }

    // set the server
    var ta = document.getElementsByName(COON_FIELD_SERVER);
    var fieldServer = ta[0];
    fieldServer.value = response.server;

    // set show player names
    var tb = document.getElementsByName(COON_FIELD_NAME);
    var fieldNames = tb[0];
    fieldNames.value = response.shownames;

    // get the textarea and autofill the report
    var tc = document.getElementsByName(COON_FIELD_REPORT);
    var fieldReport = tc[0];
    fieldReport.innerHTML = response.report;

    // should we autosubmit the form?
    if(response.autosubmit == "on") {
        // find the send button
        var inputList = document.getElementsByTagName("button")
        for(var i=0; i < inputList.length; i++) {
            inputList[i].click();
        }
    }
    
});